package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
        if(dto==null)return errorParam;
        //分页参数检查
        dto.checkParam();
        //2.安装名称模糊分页查询
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
        return getPage(dto, lambdaQueryWrapper);
    }

    @Override
    public ResponseResult add(AdChannel adChannel) {
        if (adChannel == null)return errorParam;
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return successParam;
    }

    @Override
    public ResponseResult edit(AdChannel adChannel) {
        if (adChannel == null)return errorParam;
        updateById(adChannel);
        return successParam;
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id == null)return errorParam;
        AdChannel channel = getById(id);
        if (channel == null)return notExit;
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }
        removeById(id);
        return successParam;
    }
}
